package serverdata;
import java.util.concurrent.locks.*;
import java.util.Map;
import java.util.TreeMap;



public class cache_AuthenticateInfo {
	private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
	private final Lock rl = rwl.readLock();
	private final Lock wl = rwl.writeLock();
	private final Map<Integer,ServerDataRecord.userAuthenticateRecord> cache= new TreeMap<Integer,ServerDataRecord.userAuthenticateRecord>();
	
	public ServerDataRecord.userAuthenticateRecord getValue(int id)
	{
		ServerDataRecord.userAuthenticateRecord uar = null;
		try
		{
			rl.lock();
			uar = this.cache.get(new Integer(id));			
		}
		catch(Exception e)
		{
			uar = null;
			System.out.println(String.format("getValue in cache_AuthenticateInfo: %d",id));
			System.out.println("exception:"+e.getMessage());
		}
		finally
		{
			rl.unlock();
		}
		
		return uar;
	}	
	
	public boolean checkEqual(int id, ServerDataRecord.userAuthenticateRecord uar)
	{
		ServerDataRecord.userAuthenticateRecord uar1 = this.getValue(id);
		boolean bret = false;
		if(uar1 != null)
		{
			try
			{
				if(uar1.imsi.equals(uar.imsi) && uar1.imei.equals(uar.imei))
					bret = true;							
			}
			catch(Exception e)
			{				
				System.out.println(String.format("getValue in cache_AuthenticateInfo: %d",id));
				System.out.println("exception:"+e.getMessage());
			}			
		}
		return bret;
	}
	
	public boolean putValue(int id, ServerDataRecord.userAuthenticateRecord uar)
	{
		try
		{
			wl.lock();
			this.cache.put(new Integer(id),uar);
			return true;
		}
		catch(Exception e)
		{			
			System.out.println(String.format("putValue in cache_AuthenticateInfo: %d",id));
			System.out.println("exception:"+e.getMessage());
			return false;
		}
		finally
		{
			wl.unlock();
		}
	}
	

}
